package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcCMDisinfectionTaskMapper;
import com.csun.cmny.provider.dao.OmcDisinfectionRecordBindMapper;
import com.csun.cmny.provider.dao.OmcDisinfectionRecordMapper;
import com.csun.cmny.provider.dao.OmcToiletDisinfectionRecordMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.CMDisinfectionConstant;
import com.csun.cmny.provider.model.domain.OmcCMDisinfectionTask;
import com.csun.cmny.provider.model.domain.OmcDisinfectionRecord;
import com.csun.cmny.provider.model.domain.OmcDisinfectionRecordBind;
import com.csun.cmny.provider.model.domain.OmcToiletDisinfectionRecord;
import com.csun.cmny.provider.model.dto.disinfection.*;
import com.csun.cmny.provider.model.vo.disinfection.*;
import com.csun.cmny.provider.service.OmcCMDisinfectionService;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
@Transactional
public class OmcCMDisinfectionServiceImpl extends BaseService<OmcToiletDisinfectionRecord> implements OmcCMDisinfectionService {

    @Resource
    private OmcDisinfectionRecordMapper omcDisinfectionRecordMapper;
    @Resource
    private OmcCMDisinfectionTaskMapper omcCMDisinfectionTaskMapper;
    @Resource
    private OmcDisinfectionRecordBindMapper omcDisinfectionRecordBindMapper;
    @Resource
    private OmcToiletDisinfectionRecordMapper omcToiletDisinfectionRecordMapper;

    @Override
    public Integer addTask(DisinfectionTaskDto disinfectionTaskDto, LoginAuthDto loginAuthDto) {

        OmcCMDisinfectionTask omcCMDisinfectionTask = new OmcCMDisinfectionTask();
        BeanUtils.copyProperties(disinfectionTaskDto,omcCMDisinfectionTask);
        omcCMDisinfectionTask.setUpdateInfo(loginAuthDto);
        if(PublicUtil.isEmpty(omcCMDisinfectionTask.getId())){

            //校验资产编号
            OmcCMDisinfectionTask omcCMDisinfectionTask1 = new OmcCMDisinfectionTask();
            omcCMDisinfectionTask1.setServiceItemNo(disinfectionTaskDto.getServiceItemNo());
            List<OmcCMDisinfectionTask> select = omcCMDisinfectionTaskMapper.select(omcCMDisinfectionTask1);
            if(PublicUtil.isNotEmpty(select)){
                if(!disinfectionTaskDto.getBuildId().equals(select.get(0).getBuildId()))
                    throw new OmcBizException(ErrorCodeEnum.OMC10032043,select.get(0).getBuildName(),select.get(0).getServiceItemNo());
            }

            return omcCMDisinfectionTaskMapper.insertSelective(omcCMDisinfectionTask);
        }else{
            return omcCMDisinfectionTaskMapper.updateByPrimaryKeySelective(omcCMDisinfectionTask);
        }
    }

    @Override
    public Integer deleteTask(String serviceItemNo) {
        OmcCMDisinfectionTask omcCMDisinfectionTask = new OmcCMDisinfectionTask();
        omcCMDisinfectionTask.setServiceItemNo(serviceItemNo);
        List<OmcCMDisinfectionTask> select = omcCMDisinfectionTaskMapper.select(omcCMDisinfectionTask);

        if(PublicUtil.isEmpty(select)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032039,serviceItemNo);
        }

        int result=0;
        for(OmcCMDisinfectionTask task:select){
            result+=omcCMDisinfectionTaskMapper.delete(task);
        }

        return result;
    }

    @Override
    public List<OmcDisinfectionTaskVo> selectTask(DisinfectionTaskDto disinfectionTaskDto) {
        List<OmcDisinfectionTaskVo> omcDisinfectionTaskVos = new ArrayList<>();

        List<OmcCMDisinfectionTask> omcCMDisinfectionTasks = omcCMDisinfectionTaskMapper.selectTask(disinfectionTaskDto);

        //返回格式处理
        if(PublicUtil.isNotEmpty(omcCMDisinfectionTasks)){
            //存储所有资产编号
            List<String> serviceItemNos=new ArrayList<>();
            for(OmcCMDisinfectionTask omcCMDisinfectionTask:omcCMDisinfectionTasks){
                if(!serviceItemNos.contains(omcCMDisinfectionTask.getServiceItemNo())){
                    serviceItemNos.add(omcCMDisinfectionTask.getServiceItemNo());
                }
            }


            for(String s:serviceItemNos){
                OmcCMDisinfectionTask omcCMDisinfectionTask = new OmcCMDisinfectionTask();
                omcCMDisinfectionTask.setServiceItemNo(s);
                //查询该资产编号消毒任务
                List<OmcCMDisinfectionTask> select = omcCMDisinfectionTaskMapper.select(omcCMDisinfectionTask);


                OmcDisinfectionTaskVo omcDisinfectionTaskVo = new OmcDisinfectionTaskVo();
                BeanUtils.copyProperties(select.get(0),omcDisinfectionTaskVo);

                //添加消毒具体区域
                ArrayList<DisinfectionAreaVo> disinfectionAreaVos = new ArrayList<>();
                for(OmcCMDisinfectionTask task:select){
                    DisinfectionAreaVo disinfectionAreaVo = new DisinfectionAreaVo();
                    disinfectionAreaVo.setTaskId(task.getId());
                    disinfectionAreaVo.setDisinfectionArea(task.getDisinfectionArea());
                    disinfectionAreaVos.add(disinfectionAreaVo);
                }

                omcDisinfectionTaskVo.setDisinfectionAreaVos(disinfectionAreaVos);

                omcDisinfectionTaskVos.add(omcDisinfectionTaskVo);
            }
        }

        return omcDisinfectionTaskVos;
    }

    //查询便器资产列表
    @Override
    public List<OmcCMDisinfectionTask> selectToileTask(DisinfectionTaskDto disinfectionTaskDto) {
        return omcCMDisinfectionTaskMapper.selectTask(disinfectionTaskDto);
    }

    @Override
    public Integer add(DisinfectionRecordDto disinfectionRecordDto, LoginAuthDto loginAuthDto) {
        //查询是否存在该资产
        OmcCMDisinfectionTask omcCMDisinfectionTask1 = new OmcCMDisinfectionTask();
        omcCMDisinfectionTask1.setServiceItemNo(disinfectionRecordDto.getServiceItemNo());
        List<OmcCMDisinfectionTask> tasks = omcCMDisinfectionTaskMapper.select(omcCMDisinfectionTask1);

        if(PublicUtil.isEmpty(tasks)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032039,disinfectionRecordDto.getServiceItemNo());
        }

        //查询当天是否已有消毒记录
        disinfectionRecordDto.setStartQueryTime(DateUtil.getStartTimeOfDay());
        disinfectionRecordDto.setEndQueryTime(DateUtil.getEndTimeOfDay());
        List<OmcDisinfectionRecordVo> omcDisinfectionRecordVos = omcDisinfectionRecordMapper.selectRecord(disinfectionRecordDto);

        long recordId=0;
        if(PublicUtil.isNotEmpty(omcDisinfectionRecordVos)){
            recordId=omcDisinfectionRecordVos.get(0).getRecordId();

            //若传了照片则更新
            if(PublicUtil.isNotEmpty(disinfectionRecordDto.getImage())){
                OmcDisinfectionRecord omcDisinfectionRecord = omcDisinfectionRecordMapper.selectByPrimaryKey(recordId);
                String newImage=omcDisinfectionRecord.getImage()+";"+disinfectionRecordDto.getImage();
                omcDisinfectionRecord.setImage(newImage);
                omcDisinfectionRecord.setUpdateInfo(loginAuthDto);

                omcDisinfectionRecordMapper.updateByPrimaryKeySelective(omcDisinfectionRecord);
            }

        }else {
            //没有则添加一条消毒记录
            OmcDisinfectionRecord omcDisinfectionRecord = new OmcDisinfectionRecord();
            BeanUtils.copyProperties(disinfectionRecordDto,omcDisinfectionRecord);
            omcDisinfectionRecord.setUpdateInfo(loginAuthDto);
            int i = omcDisinfectionRecordMapper.insertSelective(omcDisinfectionRecord);

            recordId=omcDisinfectionRecord.getId();
        }

        int result=0;

        //添加绑定消毒小项
        if(recordId!=0){
            for(Integer taskId:disinfectionRecordDto.getTaskIds()){
                OmcCMDisinfectionTask omcCMDisinfectionTask = omcCMDisinfectionTaskMapper.selectByPrimaryKey(taskId);

                if(PublicUtil.isEmpty(omcCMDisinfectionTask)||!omcCMDisinfectionTask.getServiceItemNo().equals(disinfectionRecordDto.getServiceItemNo())){
                    throw new OmcBizException(ErrorCodeEnum.OMC10032042,disinfectionRecordDto.getServiceItemNo(),taskId);
                }

                OmcDisinfectionRecordBind omcDisinfectionRecordBind = new OmcDisinfectionRecordBind();
                omcDisinfectionRecordBind.setRecordId(recordId);
                omcDisinfectionRecordBind.setTaskId(taskId);
                List<OmcDisinfectionRecordBind> select = omcDisinfectionRecordBindMapper.select(omcDisinfectionRecordBind);

                if(PublicUtil.isEmpty(select)){
                    omcDisinfectionRecordBind.setDisinfectionArea(omcCMDisinfectionTask.getDisinfectionArea());
                    result+=omcDisinfectionRecordBindMapper.insertSelective(omcDisinfectionRecordBind);
                }
            }
        }

        return result;
    }



    @Override
    public List<OmcDisinfectionRecordVo> selectRecord(DisinfectionRecordDto disinfectionRecordDto) {
        List<OmcDisinfectionRecordVo> omcDisinfectionRecordVos = omcDisinfectionRecordMapper.selectRecord(disinfectionRecordDto);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        if(PublicUtil.isNotEmpty(omcDisinfectionRecordVos)){
            for(OmcDisinfectionRecordVo vo:omcDisinfectionRecordVos){
                //消毒日期
                vo.setDisinfectionDate(simpleDateFormat.format(vo.getCreatedTime()));

                //消毒具体区域
                OmcDisinfectionRecordBind omcDisinfectionRecordBind = new OmcDisinfectionRecordBind();
                omcDisinfectionRecordBind.setRecordId(vo.getRecordId());
                List<OmcDisinfectionRecordBind> select = omcDisinfectionRecordBindMapper.select(omcDisinfectionRecordBind);

                vo.setOmcDisinfectionRecordBinds(select);
            }
        }

        return omcDisinfectionRecordVos;
    }


    @Override
    public Integer addToilet(ToiletDisinfectionRecordDto toiletDisinfectionRecordDto) {
        //查询是否有记录
       /* toiletDisinfectionRecordDto.setStartQueryTime(DateUtil.getStartTimeOfDay());
        toiletDisinfectionRecordDto.setEndQueryTime(DateUtil.getEndTimeOfDay());
        toiletDisinfectionRecordDto.setDisinfectionDate(DateUtil.getDate_dtShort());*/

        OmcToiletDisinfectionRecord omcToiletDisinfectionRecord = new OmcToiletDisinfectionRecord();
        omcToiletDisinfectionRecord.setOldmanId(toiletDisinfectionRecordDto.getOldmanId());
        omcToiletDisinfectionRecord.setDisinfectionDate(DateUtil.getDate_dtShort());
        List<OmcToiletDisinfectionRecord> omcToiletDisinfectionRecords = omcToiletDisinfectionRecordMapper.select(omcToiletDisinfectionRecord);

        BeanUtils.copyProperties(toiletDisinfectionRecordDto,omcToiletDisinfectionRecord);
        omcToiletDisinfectionRecord.setOldmanId(toiletDisinfectionRecordDto.getOldmanId());
        omcToiletDisinfectionRecord.setDisinfectionDate(DateUtil.getDate_dtShort());
        //有则更新
        if(PublicUtil.isNotEmpty(omcToiletDisinfectionRecords)){
            omcToiletDisinfectionRecord.setId(omcToiletDisinfectionRecords.get(0).getId());
            return omcToiletDisinfectionRecordMapper.updateByPrimaryKeySelective(omcToiletDisinfectionRecord);
        }

        return save(omcToiletDisinfectionRecord);
    }

    @Override
    public List<OmcToiletDisinfectionRecord> selectToiletRecord(ToiletDisinfectionRecordDto toiletDisinfectionRecordDto) {
        return omcToiletDisinfectionRecordMapper.selectToiletRecord(toiletDisinfectionRecordDto);
    }

    /**
     * 查询便器消毒记录
     *
     * @param huaLongToiletDownDto the hua long toilet down dto
     *
     * @return the list.
     */
    @Override
    public List<HuaLongToiletDsftRecordVo> queryToiletDsftRecord(HuaLongToiletDownDto huaLongToiletDownDto) {

        return omcToiletDisinfectionRecordMapper.queryToiletDsftRecord(huaLongToiletDownDto);
    }

    @Override
    public List<RefrigeratorAndMicrowaveDisinfectionRecordVo> selectRefrigeratorAndMicrowaveDisinfectionRecord(RefrigeratorAndMicrowaveDisinfectionRecordSelectDto selectDto) {
        return omcDisinfectionRecordMapper.selectRefrigeratorAndMicrowaveDisinfectionRecord(selectDto);
    }
}

